/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chiabai_total;

import ControlGui.DrawCard;
import Network.Connection;
import Network.ReadWrite;
import heartnew.Card;
import java.awt.Desktop;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author Oho
 */
public class main extends javax.swing.JFrame {
//    private ArrayList<Card>arr;

    /**
     * Creates new form main
     */
    //JLabel lb;
    private int idplayer;
    private DrawCard dr;
    boolean flag;//dung de xac dinh thang chia bai la thang nao 
    private int iboard;//ván thứ i
    private int idplay;//id nguoi duoc quyen danh 
    // neu id play == idplayer thi nguoi do duoc quyen danh 
    //nguoc lai hok duoc danh 
    ReadWrite rw;
    
    public main() {
        initComponents();
        this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        
    }
//flag : co chia bai ko 

    public main(boolean flag, ReadWrite rw, int iboard, int idplayer) {
        initComponents();
        this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        dr = new DrawCard(this);
        dr.InitCard();
        this.idplayer = idplayer;
        this.iboard = iboard;
        this.flag = flag;
        this.rw = rw;
        if (flag == true) {
            btnBegin.setEnabled(true);
        }
    }
    progressBar p;

    public void setValueProgressBar() {
        p = new progressBar(proBar);
        p.start();
    }
//        lb2 = new RotatedJLabel(new ImageIcon("C:/abc/2.jpg"));
//        this.add(lb2);
//        lb2.setBounds(10, 0, 100, 100);
//        lb2.setRotation(0);

    //   pn = new game1.NewJPanel();
    //  this.add(pn);
    //  pn.setBounds(0,0,100,100);
//    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnBegin = new javax.swing.JButton();
        Play = new javax.swing.JButton();
        btnMove = new javax.swing.JButton();
        btnSort = new javax.swing.JButton();
        btnRight = new javax.swing.JButton();
        txtRight = new javax.swing.JTextField();
        btnTop = new javax.swing.JButton();
        btnLeft = new javax.swing.JButton();
        txtTop = new javax.swing.JTextField();
        txtLeft = new javax.swing.JTextField();
        btnGom = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btnGomTop = new javax.swing.JButton();
        btnGomLeft = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        btnConnect = new javax.swing.JButton();
        proBar = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnBegin.setText("Begin");
        btnBegin.setEnabled(false);
        btnBegin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBeginActionPerformed(evt);
            }
        });

        Play.setText("Play");
        Play.setToolTipText("");
        Play.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayActionPerformed(evt);
            }
        });

        btnMove.setText("Move");
        btnMove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveActionPerformed(evt);
            }
        });

        btnSort.setText("Sort");
        btnSort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSortActionPerformed(evt);
            }
        });

        btnRight.setText("Right");
        btnRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRightActionPerformed(evt);
            }
        });

        btnTop.setText("Top");
        btnTop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTopActionPerformed(evt);
            }
        });

        btnLeft.setText("Left");
        btnLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLeftActionPerformed(evt);
            }
        });

        btnGom.setText("GomBottom");
        btnGom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGomActionPerformed(evt);
            }
        });

        jButton2.setText("Gom Right");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        btnGomTop.setText("Gom Top");
        btnGomTop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGomTopActionPerformed(evt);
            }
        });

        btnGomLeft.setText("Gom Left");
        btnGomLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGomLeftActionPerformed(evt);
            }
        });

        jButton3.setText("Go!");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Chat");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        btnConnect.setText("Kết Nối");
        btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectActionPerformed(evt);
            }
        });

        proBar.setMaximum(10);
        proBar.setToolTipText("");

        jButton1.setText("ChangeLeft");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 372, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnBegin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Play, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnMove, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSort, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnGom, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnGomTop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnGomLeft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(proBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(btnTop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btnRight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtRight, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                            .addComponent(txtTop)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnLeft)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtLeft)))
                                .addGap(42, 42, 42)
                                .addComponent(jButton1)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(btnBegin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Play)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnMove)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGom)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGomTop)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGomLeft)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap(39, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRight)
                    .addComponent(txtRight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTop)
                    .addComponent(txtTop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLeft)
                    .addComponent(txtLeft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnConnect)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(proBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    int i = 0;
    
    public void Reset() {
        dr.ResetCard();
    }
    
    private void btnBeginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBeginActionPerformed
        try {
            // TODO add your handling code here:

            rw.Write("chiabai");

            // for (int i = 0; i < 13; i++) {
            // for (int i = 0; i < 13; i++) {

            // for (int i = 0; i < 2; i++) {
            //        ThreadMain tm = new ThreadMain(arrimg);
            //      tm.start();

            //            tim.add(tm);
            // }
            //        DieuKhien dk = new DieuKhien(tim);
            //
            //        dk.start();
            //            this.repaint();
            //       /      img = new ThreadImages(rtt);
            //  img.start();
            //        this.repaint();

            // }
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnBeginActionPerformed
    Card c = null;
    
    private int LocationCard() {
        for (int i = 0; i < dr.getArr().size(); i++) {
            if (dr.getArr().get(i).getNumber() == dr.getTemp().getNumber() && dr.getArr().get(i).getRank() == dr.getTemp().getRank()) {
                c = dr.getArr().get(i);
                return i;
                
            }
        }
        return -1;
    }
    private void PlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayActionPerformed
        if (idplay == idplayer) {
            try {
                // TODO add your handling code here:
                p.stop();
                proBar.setValue(0);
                int vt = LocationCard();
                if (vt != -1) {
                    rw.Write("danhquanbai");
                    rw.Write(vt);//vi tri danh quan bai trong tay .
                    rw.Write(getIdplayer());//client gửi wa server cái mã người chơi
                    rw.Write(c);//client gửi wa server lá bài mà người chơi đó đánh ra 
                    //      System.out.printf("nhap vao la bai "+lb.getNumber()+","+lb.getRank());
                    //      System.out.printf("nhap vao la bai "+lb.getNumber()+","+lb.getRank());
                }
                getDr().ShowInfo();
            } catch (IOException ex) {
                Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showMessageDialog(this, "chua den luot");
        }
        
    }//GEN-LAST:event_PlayActionPerformed
    private void btnMoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveActionPerformed
        getDr().ShowInfo();
    }//GEN-LAST:event_btnMoveActionPerformed
    
    private void btnSortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSortActionPerformed
        // TODO add your handling code here:
//        getDr().Sort();
    }//GEN-LAST:event_btnSortActionPerformed
    
    private void btnRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRightActionPerformed
        // TODO add your handling code here:
        getDr().pushCardRight(Integer.parseInt(txtRight.getText()), null);
    }//GEN-LAST:event_btnRightActionPerformed
    
    private void btnTopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTopActionPerformed
        // TODO add your handling code here:
        getDr().pushCardTop(Integer.parseInt(txtTop.getText()), null);
    }//GEN-LAST:event_btnTopActionPerformed
    
    private void btnLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLeftActionPerformed
        // TODO add your handling code here:

        getDr().pushCardLeft(Integer.parseInt(txtLeft.getText()), null);
    }//GEN-LAST:event_btnLeftActionPerformed
    
    private void btnGomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGomActionPerformed
        // TODO add your handling code here:
        getDr().AcessWinButtom();
    }//GEN-LAST:event_btnGomActionPerformed
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        getDr().AcessWinRight();
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void btnGomTopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGomTopActionPerformed
        // TODO add your handling code here:
        getDr().AcessWinTop();
    }//GEN-LAST:event_btnGomTopActionPerformed
    
    private void btnGomLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGomLeftActionPerformed
        // TODO add your handling code here:
        getDr().AcessWinLeft();
    }//GEN-LAST:event_btnGomLeftActionPerformed
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        getDr().changleCards();
    }//GEN-LAST:event_jButton3ActionPerformed
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed
    
    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed

        // TODO add your handling code here:

        new Connection(1234).start();

        //viet ham ket noi cho su kien nay .
        //        ket noi den server goi ket noi den server
        //1. lop connection.java extent thread
        //public void run(){new readwrite(in,out).start()}
        //2. lop read/write extent thread//
        // su kien run(){while true read(); }
        //                cnn.start()
        //                cnn.start()

    }//GEN-LAST:event_btnConnectActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                    
                    
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(main.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(main.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(main.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(main.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Play;
    private javax.swing.JButton btnBegin;
    private javax.swing.JButton btnConnect;
    private javax.swing.JButton btnGom;
    private javax.swing.JButton btnGomLeft;
    private javax.swing.JButton btnGomTop;
    private javax.swing.JButton btnLeft;
    private javax.swing.JButton btnMove;
    private javax.swing.JButton btnRight;
    private javax.swing.JButton btnSort;
    private javax.swing.JButton btnTop;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JProgressBar proBar;
    private javax.swing.JTextField txtLeft;
    private javax.swing.JTextField txtRight;
    private javax.swing.JTextField txtTop;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the dr
     */
    public DrawCard getDr() {
        return dr;
    }

    /**
     * @param dr the dr to set
     */
    public void setDr(DrawCard dr) {
        this.dr = dr;
    }

    /**
     * @return the idplayer
     */
    public int getIdplayer() {
        return idplayer;
    }

    /**
     * @param idplayer the idplayer to set
     */
    public void setIdplayer(int idplayer) {
        this.idplayer = idplayer;
    }

    /**
     * @return the iboard
     */
    public int getIboard() {
        return iboard;
    }

    /**
     * @param iboard the iboard to set
     */
    public void setIboard(int iboard) {
        this.iboard = iboard;
    }

    /**
     * @return the idplay
     */
    public int getIdplay() {
        return idplay;
    }

    /**
     * @param idplay the idplay to set
     */
    public void setIdplay(int idplay) {
        this.idplay = idplay;
    }
}
